<template>
  <div class="app-container">
    <!-- <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-permisaction="['community:comment:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >评论</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          v-permisaction="['community:comment:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postingCommentList"
      @selection-change="handleSelectionChange"
      row-key="commentId"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="用户"
        align="center"
        prop="userInfo"
        :show-overflow-tooltip="true"
        width="180"
      >
        <template slot-scope="scope">{{scope.row.userInfo.nickname}}</template>
      </el-table-column>
      <el-table-column
        label="评论内容"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
        width="820"
      >
        <template slot-scope="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>
      <el-table-column
        label="评论时间"
        align="center"
        prop="createdAt"
        :show-overflow-tooltip="true"
        width="160"
      />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-permisaction="['community:comment:add']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAddComment(scope.row)"
          >回复</el-button>
          <el-button
            v-permisaction="['community:comment:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      style="align-items:flex-start"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="authorId">
          <div v-if="form.commentId">
            <el-avatar :src="form.userInfo.avatar"></el-avatar>
            {{form.userInfo.nickname}}
          </div>
          <div v-else>
            <el-select v-model="form.userId" filterable placeholder="请选择官方用户">
              <el-option
                v-for="item in officeuserOptions"
                :key="item.userId"
                :label="item.nickname"
                :value="item.userId"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
          <quill-editor v-model="form.content" ref="content" class="editer"></quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listpostingComment,
  getpostingComment,
  delpostingComment,
  addpostingComment,
  updatepostingComment,
} from "@/api/community/comment";

//调用编辑器
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

export default {
  props: {
    origin: {
      type: String,
      default: () => {
        return "";
      },
    },
    originId: {
      type: Number,
      default: () => {
        return 0;
      },
    },
  },
  components: {
    //使用编辑器
    quillEditor,
  },
  name: "comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
      },
      queryData: {
        origin: "",
        originId: 0,
      },
      // 表单参数
      form: {
        parentId: 0,
        userInfo: {
          avatar: "",
          nickname: "",
        },
        originId: this.originId,
        origin: this.origin,
        commentId: undefined,
      },
      // 表单校验
      rules: {},
      //评论列表
      postingCommentList: [],
      //官方用户列表
      officeuserOptions: [],
    };
  },
  created() {
    this.getList();
    this.officeuserOptions = this.$store.getters.parameter.general_user_office;
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill;
    },
  },
  watch: {
    //监听子控件内容的改动,发送给父级组件
    originId: {
      handler(newValue, oldValue) {
        this.getList();
      },
      deep: true,
    },
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      this.queryData.origin = this.origin;
      this.queryData.originId = this.originId;
      listpostingComment(
        this.addDateRange(this.queryParams, this.dateRange),
        this.queryData
      ).then((response) => {
        this.postingCommentList = response.data.list;
        this.total = response.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        parentId: 0,
        commentId: undefined,
        authorId: undefined,
        commentContent: undefined,
        postingId: this.postingId,
        userInfo: {
          avatar: "",
          nickname: "",
        },
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评论";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.commentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 回复按钮操作 */
    handleAddComment(row) {
      this.reset();
      console.log(row);
      this.form.parentId = row.commentId;
      this.open = true;
      this.title = "回复：" + row.userInfo.nickname;
      this.form.originId = row.originId;
      // getpostingComment(commentId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改评论";
      // });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.origin = this.origin;
      if (this.originId > 0) {
        this.form.originId = this.originId;
      }

      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.commentId !== undefined) {
            updatepostingComment(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addpostingComment(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.commentId || this.ids;
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delpostingComment(Ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>